package com.dhcc.dao.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.dhcc.dao.UserManagerDao;
import com.dhcc.model.Student;
import com.dhcc.model.Teacher;
import com.dhcc.model.User;
import com.dhcc.model.UserInsert;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.Default;

@Repository("userManagerDao")
public class UserManagerImpl extends BaseDaoImpl<User> implements UserManagerDao{
		
	@Autowired
	private JdbcTemplate jdbcTemplate;
	//����User
		@Override
		public void update(User user) {
			Method method = null;
			try {
				method = clazz.getDeclaredMethod("getUserId");

				//ͨ��ID��ѯT����
				User existUser = (User) entityManager.find(clazz, method.invoke(user));
				//����ѯ��ѯ�ɹ���
				if (existUser != null) {
					//merge()�ᴴ��һ������ĸ�����Ȼ�������������в������
					entityManager.merge(existUser);
				}
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		@Override
		public void delete(User user) {
			Method method = null;

			try {
				method = clazz.getDeclaredMethod("getId");

				User existUser = (User) entityManager.find(clazz, method.invoke(user));

				if (existUser != null) {

					entityManager.remove(existUser);
				}

			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		
		/**
		 * 根据用户信息查询信息
		 */
		@Override
		public Student getStudentByUserId(int id) {
			String sqlStr="SELECT * FROM tb_student WHERE stu_id=?";
			final Student bean=new Student();
			jdbcTemplate.query(sqlStr, new Object[]{id},
	                new RowCallbackHandler() {
	            public void processRow(ResultSet rs) throws SQLException {
	               bean.setStuId(rs.getInt("stu_id"));
	               bean.setStuName(rs.getString("stu_name"));
	               bean.setSex(rs.getString("sex"));
	               bean.setAge(rs.getByte("age"));
	               bean.setPhone(rs.getString("phone"));
	               bean.setEmail(rs.getString("email"));
	               bean.setCollege(rs.getString("college"));
	               bean.setCompany(rs.getString("company"));
	               bean.setArriveTime(rs.getString("arrive_time"));
	               bean.setLeaveTime(rs.getString("leave_time"));
	               bean.setUserId(rs.getInt("user_id"));
	            }
	        });
			return bean;
		}
		
		@Override
		public User getUserByUserId(int userId) {
			String sqlStr="SELECT * FROM tb_user WHERE user_id=?";
			final User bean=new User();
			jdbcTemplate.query(sqlStr, new Object[]{userId},
	                new RowCallbackHandler() {
	            public void processRow(ResultSet rs) throws SQLException {
	               bean.setUserId(rs.getInt("user_id"));
	               bean.setUserType(rs.getString("user_type"));
	               bean.setPassword(rs.getString("passWord"));
	            }
	        });
			return bean;
		}
		/**
		 * 更新学生信息
		 */
		@Override
		public void upStudentInfo(Student s) {
			String sqlStr="UPDATE  tb_student SET stu_name=?,sex=?,age=?," +
					"phone=?,email=?,college=?,company=?,arrive_time=?," +
					"leave_time=? WHERE stu_id=?";
			Object[] obj=new Object[]{s.getStuName(),s.getSex(),s.getAge(),
					s.getPhone(),s.getEmail(),s.getCollege(),s.getCompany(),
					s.getArriveTime(),s.getLeaveTime(),s.getStuId()};
			jdbcTemplate.update(sqlStr, obj);
		}
		/**
		 * 更新用户信息
		 */
		@Override
		public void upUserInfo(User u) {
			String sqlStr="UPDATE  tb_user SET user_type=?,password=?,email=? WHERE user_id=?";
			Object[] obj=new Object[]{u.getUserType(),u.getPassword(),u.getEmail(),u.getUserId()};
			jdbcTemplate.update(sqlStr, obj);
			
		}
		
		/**
		 * 查询学生的信息
		 */
		@Override
		public List<Student> selectStudent1(int page) {
			int pageSize=10;
			int startRow=(page-1)*pageSize;
			String sqlStr="FROM Student";
			List list = entityManager.createQuery(sqlStr).setFirstResult(startRow).setMaxResults(10).getResultList();
		    return list;
		}
		/**
		 * 查询老师的信息
		 */
		@Override
		public List<Teacher> selectTeacher1(int page) {
			int pageSize=10;
			int startRow=(page-1)*pageSize;
			String sqlStr="FROM Teacher";
			List list = entityManager.createQuery(sqlStr).setFirstResult(startRow).setMaxResults(10).getResultList();
		    return list;
		}
		/**
		 * 添加老师信息
		 */
		@Override
		public void insertTeacher1(UserInsert insert) {
			String sqlStr="insert into tb_teachervalues(?,?,?,?,?,?,?)";
			Object[] obj=new Object[]{insert.getInsId(),insert.getInsName(),insert.getInsSex(),insert.getInsPhone(),insert.getInsEmail(),insert.getInsCompany(),insert.getUserId()};
			jdbcTemplate.update(sqlStr, obj);
		}
		/**
		 * 添加学生信息
		 */
		@Override
		public void insertStudent1(UserInsert insert) {
			String sqlStr="insert into tb_student (stu_id,stu_name,sex,phone,email,company,user_id) values(?,?,?,?,?,?,?)";
			Object[] obj=new Object[]{insert.getInsId(),insert.getInsName(),insert.getInsSex(),insert.getInsPhone(),insert.getInsEmail(),insert.getInsCompany(),insert.getUserId()};
			jdbcTemplate.update(sqlStr, obj);
			
		}
		
		
}
